﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XEnamel.Python;

namespace XEnamel.Tests
{
	[TestClass]
	public class PythonTests
	{
		[TestMethod]
		public void Can_translate_pow()
		{
			var context = new PythonContext();
			context.ExecuteSource("def pow(x): return x**2");

			var func = context.GetVariable<Func<double, double>>("pow");
			var transFunc = new PythonTranslator().VisitAndCompile(context.GetVariable("pow"));

			Assert.IsNotNull(transFunc);
			foreach (var n in Enumerable.Range(1, 100))
			{
				Assert.AreEqual(func(n), transFunc(n));
			}
		}

		[TestMethod]
		public void Can_translate_fibo()
		{
			var context = new PythonContext();
			context.ExecuteSource(
				"def fibo(x):\n" +
				"\tc=[]\n" +
				"\ta, b=0, 1\n" +
				"\twhile b<x:\n" +
				"\t\tc.append(b)\n" +
				"\t\ta, b=b, a+b\n" +
				"\treturn c");

			var func = context.GetVariable<Func<double, dynamic>>("fibo");
			var list = func(20);
			Assert.AreEqual(7, list.Count);
			Assert.AreEqual(1, list[0]);
			Assert.AreEqual(1, list[1]);
			Assert.AreEqual(2, list[2]);
			Assert.AreEqual(3, list[3]);
			Assert.AreEqual(5, list[4]);
			Assert.AreEqual(8, list[5]);
			Assert.AreEqual(13, list[6]);

			var transFunc = new PythonTranslator().VisitAndCompile(context.GetVariable("fibo"));
			Assert.IsNotNull(transFunc);

			var transList = transFunc(20);
			Assert.AreEqual(list.Count, transList.Count);
			for (int index = 0; index < list.Count; index++)
			{
				Assert.AreEqual(list[index], transList[index]);
			}
		}
	}
}